import { FileTableCol } from '@/views/dataSheet/DataConfig/FileTable'
import typeOfUtils from '@/utils/type-of-utils'
import { RulesItemValidator } from '@/components/EditDataTable/elementConfig'
import { DataType } from '@/config/PageConfig'

export default class FormValidator {
  public static nameValidator(list: FileTableCol[] = []): RulesItemValidator<FileTableCol> {
    return function(rule, value, callback, { rowIndex }) {
      if (typeOfUtils.isEmpty(value)) {
        callback(new Error('字段不能为空'))
        return
      }
      const find = list.findIndex(i => i.name === value)
      if (find !== -1 && find !== rowIndex) {
        callback(new Error(`字段重名: 第${find + 1}行`))
        return
      }
      callback()
    }
  }

  public static lengthValidator(dataType: string): RulesItemValidator<FileTableCol> {
    return function(rule, value, callback, other) {
      if (typeOfUtils.isEmpty(value)) {
        callback()
      } else if (!/\d+|\d+,([-+])?\d+/.test(value)) {
        callback(new Error('数据格式错误, 数据类型只能是: 全[数字]或者是[数字,数字]'))
      } else if (DataType.ORACLE.value === dataType) {
        callback()
      } else if (DataType.MYSQL.value === dataType) {
        callback()
      } else {
        callback()
      }
    }
  }

  public static typeValidator(): RulesItemValidator<FileTableCol> {
    return function(rule, value, callback, other) {
      if (typeOfUtils.isEmpty(value)) {
        callback(new Error('不能为空'))
      } else {
        callback()
      }
    }
  }
}
